import pymongo
from pyecharts import options as opts
from pyecharts.charts import Page, Pie


db = pymongo.MongoClient('localhost',27017)
douban_db = db['doubantop250']
item_info = douban_db['movietop250']

country_list = []

"""
        构建top250电影中出自国家最多的国家的列表；
"""

for i in item_info.find():
    country_list.append(str(i['country']).strip())


country_type = ['美国', '中国', '日本', '韩国', '法国', '德国', '印度', '意大利', '澳大利亚', '新西兰', '瑞士', '加拿大']
count_list = []
for j in range(len(country_type)):
    count = 0
    for i in country_list:
        if country_type[j] in i:
            count += 1
    count_list.append(count)


pie = Pie()
pie.add(
    "",
    [z for z in zip(country_type, count_list)],
    center=["50%", "50%"],
)

pie.set_global_opts(
    title_opts=opts.TitleOpts(title="豆瓣top250电影产源国家数量占比"),
    legend_opts=opts.LegendOpts(orient="vertical", pos_top="15%", pos_left="6%")
)

pie.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))

pie.render('mycountry1.0.html')